<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */

/**
 * Interest Service class for communication between controllers and persistent storage
 *
*/
class Core_Service_Interest extends RedCross_Service_ServiceAbstract
{
    
    public function __construct(Core_Gateway_Interest $gateway)
    {
        parent::__construct($gateway, 'Core_Model_Interest');
    }

    public function getValidator()
    {

    }
    
    public function fetchAllInterestsForMember($memberId)
    {
        return $this->getGateway()->fetchAllInterestsForMember($memberId);
    }
    
    public function toggleInterest($memberId,$eventId)
    {
        //$authService = $this->_helper->authenticator();
        //$memberId = $authService->getIdentity()->member_id;
        
        $hasInterest = $this->getGateway()->checkIfMemberHasInterestForEvent($memberId, $eventId);
        
        if($hasInterest != null){
            if($this->getGateway()->deleteInterestFromEventForMember($memberId, $eventId)){
                return false;
            } else {
                throw new RedCross_Exception('Failed to delete interest');
            }
        } else {
            $interest = new Core_Model_Interest();
            $interest->member_id = $memberId;
            $interest->event_id = $eventId;
            
            if($this->save($interest)){
               return true; 
            } else {
                throw new RedCross_Exception('Failed to save interest');
            }
            
        }
    }
    public function checkIfMemberHasInterestForEvent($memberId, $eventId)
    {
        return $this->getGateway()->checkIfMemberHasInterestForEvent($memberId, $eventId);
    }
}